//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbBangDiem
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbBangDiem_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbBangDiem_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbBangDiem OTbBangDiem)
        {
            SqlParameter[] pr = new SqlParameter[4];
            pr[0] = new SqlParameter(@"MaHV", OTbBangDiem.MaHV);
            pr[1] = new SqlParameter(@"MaMonHoc", OTbBangDiem.MaMonHoc);
            pr[2] = new SqlParameter(@"DiemTK", OTbBangDiem.DiemTK);
            pr[3] = new SqlParameter(@"Namhoc", OTbBangDiem.Namhoc);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbBangDiem_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbBangDiem OTbBangDiem)
        {
            SqlParameter[] pr = new SqlParameter[4];
            pr[0] = new SqlParameter(@"MaHV", OTbBangDiem.MaHV);
            pr[1] = new SqlParameter(@"MaMonHoc", OTbBangDiem.MaMonHoc);
            pr[2] = new SqlParameter(@"DiemTK", OTbBangDiem.DiemTK);
            pr[3] = new SqlParameter(@"Namhoc", OTbBangDiem.Namhoc);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbBangDiem_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(string MaHV,int MaMonHoc)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaHV",MaHV);
            pr[1] = new SqlParameter(@"MaMonHoc",MaMonHoc);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbBangDiem_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbBangDiem SelectByID(string MaHV,int MaMonHoc)
        {
            ETbBangDiem OTbBangDiem = new ETbBangDiem();
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaHV",MaHV);
            pr[1] = new SqlParameter(@"MaMonHoc",MaMonHoc);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbBangDiem_SelectByID", pr);
            if (idr.Read())
              OTbBangDiem = GetOneTbBangDiem(idr);
            idr.Close();
            idr.Dispose();
            return OTbBangDiem;
        }
//-----------------------------------------------------------//
        private static ETbBangDiem GetOneTbBangDiem(IDataReader idr)
       {
            ETbBangDiem OTbBangDiem = new ETbBangDiem();
                if (idr["MaHV"] != DBNull.Value)
                    OTbBangDiem.MaHV = (string)idr["MaHV"];
                if (idr["MaMonHoc"] != DBNull.Value)
                    OTbBangDiem.MaMonHoc = (int)idr["MaMonHoc"];
                if (idr["DiemTK"] != DBNull.Value)
                    OTbBangDiem.DiemTK = (double)idr["DiemTK"];
                if (idr["Namhoc"] != DBNull.Value)
                    OTbBangDiem.Namhoc = (string)idr["Namhoc"];
            return OTbBangDiem;
       }
//-----------------------------------------------------------//
        public static bool TestByID(string MaHV,int MaMonHoc)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaHV",MaHV);
            pr[1] = new SqlParameter(@"MaMonHoc",MaMonHoc);
            pr[2] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[2].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbBangDiem_TestByID", pr);
            return Convert.ToBoolean(pr[2].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbBangDiem> ListAll()
        {
            List<ETbBangDiem> list = new List<ETbBangDiem>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbBangDiem_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbBangDiem(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbBangDiem> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbBangDiem> list = new List<ETbBangDiem>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbBangDiem_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbBangDiem(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}